<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Rate the association with the start rating widget.
 * MUST include the jRating lib.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.1
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

require_once INSTALLDIR . '/lib/slh_jsonaction.php';

require_once INSTALLDIR . '/classes/Association.php';

class MarkjsonAction extends JsonAction
{
	/**
	 * The returning data items.
	 */
	var $status = '0';			// 0 error, 1 success
	var $message = '';			// some message
	
	/**
	 * Id of the user.
	 */
	var $user_id;
	
	/**
	 * Mark of this rating.
	 */
	var $mark;
	
	/**
	 * Association item.
	 */
	var $association_mark;
	
	function prepare($args)
	{
		parent::prepare($args);
		
		// Parameter from 'post' url
		$this->user_id = $this->getTrimmedArg('puser_id');
		$this->mark = $this->getTrimmedArg('pmark');
		
		if ( !is_numeric($this->user_id) || !is_numeric($this->mark)) {
			return false;
		}
		
		$this->mark *= 10;

		$this->association_mark = Association_Mark::staticGet('user_id', $this->user_id);
			
		// Inits the jsondata.
		$this->jsondata['status'] = $this->status;
		$this->status =& $this->jsondata['status'];
		$this->jsondata['message'] = $this->message;
		$this->message =& $this->jsondata['message'];
		
		return true;
	}
	
	/**
	 * Handle a request.
	 */
	function handle($args)
	{
		parent::handle($args);
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$this->handlePost();
		} else {
			slh_redirectto_default();
		}
		
		parent::showJson();
	}
	
	/**
	 * Handle a post, if a really poster uploading post.
	 */
	function handlePost()
	{
		if (empty($this->association_mark)) {
			$this->status = '0';
			$this->message = '没有评分对象!';
		} else {
			if(empty($_COOKIE['association'.$this->user_id])) {
				if ($this->association_mark->mark($this->mark)) {
					$this->status = '1';
					$this->message = '感谢您的评分!';
					
					// Remember the cookie.
					setcookie('association'.$this->user_id, 'marked', time() + 3600);
				} else {
					$this->status = '0';
					$this->message = '评分出错了哦!';
				}
			} else {
				$this->status = '0';
				$this->message = '收到评分谢谢!';
			}
		}
	}
}

?>